/*
 *  @(#)PersonController.java    0.3 16/04/2009
 */
package org.miao.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.miao.example.domain.Person;
import org.miao.example.repository.Repository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/**
 * 用户控制器
 * @author Miao
 * @version 0.3
 * @since 0.3
 */
@Controller
@SessionAttributes("person")
@RequestMapping("/person.htm")
public class PersonController {

    /**
     * 持久化存储对象
     */
    @Resource
    private Repository repository;

    /**
     * 自定义属性编辑器注入
     * @param binder Web数据绑定者
     * @since 0.3
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Person.class, new PersonEditor());
    }

    /**
     * 查询Person
     * @param name 名称
     * @param age 年龄
     * @param model 结果集
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=list")
    public String list(@RequestParam(required = false) String name, @RequestParam(required = false) Integer age, @RequestParam(required = false) String order, Map<String, Object> model) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        List<String> orders = new ArrayList<String>();
        if (name != null && !name.equals("")) {
            parameters.put(":name", "%" + name + "%");
        }
        parameters.put(":age", age);
        orders.add(order);
        model.put("list", repository.findByNamed("listPerson", parameters, orders));
        return "/person/list";
    }

    /**
     * 转向创建新Person页面
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=create", method=RequestMethod.GET)
    public String create(Map<String, Object> model) {
        model.put("person", new Person());
        return "/person/create";
    }

    /**
     * 创建新Person
     * @param person Person
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=create", method=RequestMethod.POST)
    public String createSubmit(@ModelAttribute("person") Person person, SessionStatus sessionStatus) {
        repository.persist(person);
        sessionStatus.setComplete();
        return "redirect:/person.htm?method=list";
    }

    /**
     * 读取待更新Person
     * @param id Person主键
     * @param model 结果集
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=update", method=RequestMethod.GET)
    public String update(@RequestParam Long id, Map<String, Object> model) {
        model.put("person", repository.find(Person.class, id));
        return "/person/update";
    }

    /**
     * 修改Person
     * @param person Person
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=update", method=RequestMethod.POST)
    public String updateSubmit(@ModelAttribute("person") Person person, SessionStatus sessionStatus) {
        repository.merge(person);
        sessionStatus.setComplete();
        return "redirect:/person.htm?method=list";
    }

    /**
     * 删除指定Person
     * @param id Person主键
     * @return 跳转路径
     * @since 0.3
     */
    @RequestMapping(params="method=delete")
    public String delete(@RequestParam Long id) {
        repository.remove(Person.class, id);
        return "redirect:/person.htm?method=list";
    }


    /**
     * 获得持久化存储对象
     * @return 持久化存储对象
     * @since 0.3
     */
    public Repository getRepository() {
        return repository;
    }

    /**
     * 设置持久化存储对象
     * @param repository 持久化存储对象
     * @since 0.3
     */
    public void setRepository(Repository repository) {
        this.repository = repository;
    }
}
